<template>
    <!-- 注册 -->
    <div class="registerBar">
        <div class="register_top">
            <div class="operationIcon operationIconHover" @click="userOperationStoreState.useLogin()">
                <el-icon :size="40" color="#73767a"><Back /></el-icon>
            </div>
            <div class="title">注册</div>
            <div class="operationIcon operationIconHover" @click="loginStoreState.isTryingLoginOfFalse()">
                <el-icon :size="40" color="#73767a"><Close /></el-icon>
            </div>
        </div>
        <div class="register_form_item">
            <form class="register_form_content">
                <div class="input_div">
                    <span class="register_span">
                        <img src="../icons/user/nickname.png" alt="" class="register_png">
                    </span>
                    <input type="text" name="nickname" class="register_input" aria-describedby="inputGroup-sizing-default" placeholder="使用昵称" v-model="nickname">
                </div>
                <div class="input_div">
                    <span class="register_span">
                        <img src="../icons/user/username.png" alt="" class="register_png">
                    </span>
                    <input type="text" name="username" class="register_input" aria-describedby="inputGroup-sizing-default" placeholder="用户名" v-model="username">
                </div>
                <div class="input_div">
                    <span class="register_span">
                        <img src="../icons/user/email.png" alt="" class="register_png">
                    </span>
                    <input type="text" name="email" class="register_input" aria-describedby="inputGroup-sizing-default" placeholder="邮箱" v-model="email">
                </div>
                <div class="input_div">
                    <span class="register_span" id="register_password">
                        <img src="../icons/user/password.png" alt="" class="register_png">
                    </span>
                    <input type="password" name="password" class="register_input" aria-describedby="inputGroup-sizing-default" placeholder="密码" v-model="password">
                </div>
                <div class="input_div">
                    <span class="register_span" id="register_password">
                        <img src="../icons/user/passwordAgain.png" alt="" class="register_png">
                    </span>
                    <input type="password" name="passwordAgain" class="register_input" aria-describedby="inputGroup-sizing-default" placeholder="再次输入密码" v-model="passwordAgain">
                </div>
                <div class="input_div">
                    <input type="text" name="verify" class="register_input_verify" aria-describedby="inputGroup-sizing-default" placeholder="验证码" v-model="verifyNum">
                    <span class="register_span_verify" id="register_verify">
                        <img :src="veriftImgSrc" alt="" class="register_verify_png" @click="changeVerifyImg">
                    </span>
                </div>
                <el-button type="button" class="submit_btn" @click="register"
                           :loading="offSticky"
                           :disabled="offSticky"
                           color="#5995fd"
                >
                    <text class="submit_text">注册</text>
                </el-button>
            </form>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { Back, Close } from '@element-plus/icons-vue'
import { loginState } from '@/stores/loginState.js'
import { userOperationState } from '@/stores/userOperationState.js'
// import { UserRegister } from '@/apis/banner.js'

// 引入全局状态登陆状态和操作状态
const loginStoreState = loginState()
const userOperationStoreState = userOperationState()

// 注册信息
const nickname = ref('')
const username = ref('')
const password = ref('')
const passwordAgain = ref('')
const email = ref('')
const verifyNum = ref('')

// 验证码变量
const randomNum = ref(Math.random())
const captchaSrc = ref('http://localhost:8080/public/captcha/register')
const veriftImgSrc = ref(captchaSrc.value)

// 其它变量
const offSticky = ref(false)

const changeVerifyImg = () => {
    randomNum.value = Math.random();
    veriftImgSrc.value = captchaSrc.value + '?randomCode=' + randomNum.value
}

const register = async () => {
    offSticky.value = true
    if(nickname === ''){
        ElMessage({
            message: '请输入昵称',
            type: 'warning',
        })
        offSticky.value = false;
        return
    } else if(username === ''){
        ElMessage({
            message: '请输入用户名',
            type: 'warning',
        })
        offSticky.value = false;
        return
    } else if(password === ''){
        ElMessage({
            message: '请输入密码',
            type: 'warning',
        })
        offSticky.value = false;
        return
    } else if(passwordAgain === ''){
        ElMessage({
            message: '请再次输入密码',
            type: 'warning',
        })
        offSticky.value = false;
        return
    } else if(email === ''){
        ElMessage({
            message: '请输入邮箱',
            type: 'warning',
        })
        offSticky.value = false;
        return
    } else if(verifyNum === ''){
        ElMessage({
            message: '请输入验证码',
            type: 'warning',
        })
        offSticky.value = false;
        return
    }

    // 判断两次输入的密码是否一致
    if(password === passwordAgain){
        ElMessage({
            message: '两次输入的密码不一致',
            type: 'warning',
        })
        offSticky.value = false;
        return
    }

    let data = {
        nickname: nickname.value,
        username: username.value,
        password: password.value,
        passwordAgain: passwordAgain.value,
        email: email.value,
        verifyNum: verifyNum.value
    }

    UserRegister(data)
    .then((result) => {
        offSticky.value = false;
        if(result.success){
            userOperationStoreState.useLogin()
        }
    })
}

</script>

<style scoped>
.registerBar{
    height: 70vh;
    width: 35%;
    background-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0px 2px 4px 0 rgba(0, 0, 0, 0.2);
    border: 1px solid #efefef;
}

.register_top{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2vh 2vh 0 2vh;
}
.title{
    font-size: 38px;
    color: #444444;
    letter-spacing: 8px;
    font-family: "Microsoft YaHei", sans-serif;
}
.operationIcon{
    height: 50px;
    width: 50px;
    border-radius: 4px;
    transition: all 0.4s ease-in-out;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.operationIconHover:hover{
    background-color: #e9e9eb;
}

.register_form_item{
    padding-top: 20px;
    height: 80%;
    width: 100%;
}
.register_form_content{
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
.input_div{
    height: 20%;
    width: 60%;
    background-color: #f0f0f0;
    display: flex;
    border-radius: 4px;
    margin-bottom: 2%;
}
.register_span{
    height: 100%;
    width: 15%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}
.register_png{
    height: 40%;
    width: 40%;
}
.register_input{
    height: 100%;
    width: 80%;
    border: none;
    background-color: rgb(0,0,0,0);
    font-size: 1.1rem;
}
.register_input_verify{
    height: 100%;
    width: 60%;
    border: none;
    background-color: rgb(0,0,0,0);
    font-size: 1.1rem;
    padding-left: 8%;
}
.register_span_verify{
    height: 100%;
    width: 40%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    cursor: pointer;
}
.register_verify_png{
    height: 100%;
    width: 100%;
    border-radius: 4px;
    background-color: #dedfe0;
    border:1px solid #b1b3b8;
}
.switch_div{
    height: auto;
    width: 60%;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.switch_jump{
    cursor: pointer;
    transition: all 0.2s ease-in-out;
}
.switch_jump:hover{
    color: #409EFF;
}

.submit_btn{
    background-color: #5995fd;
    width: 60%;
    height: 20%;
    margin-top: 1.5%;
    border-radius: 4px;
    border: none;
    transition: background-color 0.2s ease-in-out;
}
.submit_btn:hover{
    background-color: rgb(82, 106, 157);
}
.submit_btn:active{
    background-color: #3b5f9d;
}

.submit_text{
    font-size: 17px;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: #fff;
    font-weight: 600;
}
</style>
